package com.itboye.mengwu.app.ui.mymeng.personsettings;

import java.util.Observable;
import java.util.Observer;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

public class ActivityUpdatePwdByPwd extends BaseActivity implements
		OnClickListener, Observer {

	EditText input_olderpwd, input_newpwd,input_bindphone;
	Button btn_submit;
	private String newPwd;
	private String oldPwd;
	MainPresenter mainPresenter;
	private String uid;
	TextView txt_title,getYzm;
	ImageView back;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String phoneNumber;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_updatepwdbypwd;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("验证密码");
		phoneNumber=SPUtils.get(this, null, SpPublicParams.PHONE_MODEL, "")+"";
		mainPresenter = new MainPresenter(this);
		getYanzhengMaPresenter=new GetYanzhengMaPresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.btn_submit:
			oldPwd = input_bindphone.getText().toString().trim();
			if(oldPwd.isEmpty()){
				ByAlert.alert("请输入原密码");
				return;
			}
			newPwd = input_newpwd.getText().toString().trim();
			if(newPwd.isEmpty()){

				ByAlert.alert("请输入新密码");
			}
			showProgressDialog("密码更改中", true);
			btn_submit.setEnabled(false);
			mainPresenter.updatePasswordByPwd(uid, newPwd, oldPwd, "");
			break;
		case R.id.back:
			finish();
			break;
		case R.id.getYzm:
			getYanzhengMaPresenter.getVerificationCode(phoneNumber, "2");
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if(entity!=null){
			
			if(entity.getEventType()==MainPresenter.UpdatePswByPwdSuccessEvent){
				ByAlert.alert(entity.getData() + "");
				finish();
			}else if(entity.getEventType()==MainPresenter.UpdatePswByPwdFailureEvent){
				ByAlert.alert(entity.getData() + "");
			}
		}

		btn_submit.setEnabled(true);
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
